<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <div id="a"></div>
  <script>
    // a: {
    //   value: 2,    值
    //   writable: true,    可读
    //   enumerable: true,    枚举
    //   configurable: true     可设置
    // }
    function definedP(obj) {
      for (let key in obj) {
        let _key = obj[key]
        Object.defineProperty(obj, key, {
          get() {
            console.log('get')
            return _key
          },
          set(newVal) {
            console.log('set')
            let aDom = document.getElementById('a');
            a.innerHTML = newVal;
            _key = newVal
          }
        })
      }
      return obj
    }
    var obj = { a: 1, b: 2 };
    definedP(obj)
    obj.a = 2;
    obj.b = 3;
    obj.b = 1000;
    obj.b = 088;
    console.log(obj.a)
    console.log(obj.b)

  </script>
</body>

</html>